/*
 * OnCommandJuzEvent.cpp
 *
 *  Created on: 2017年5月28日
 *      Author: orange
 */

#include "OnCommandJuzEvent.h"

#include <string.h>
#include <iostream>
#include <string>

const char* OnCommandJuzEvent::mEventName = "OnCommandJuzEvent";
OnCommandJuzEvent::OnCommandJuzEvent():JuzEvent(mEventName)
{
	for(int i = 0 ; i < MAX_STR; i++)
	{
		mPstr[i] = NULL;
	}

}

OnCommandJuzEvent::~OnCommandJuzEvent() {

	checkAlloc();
}

void OnCommandJuzEvent::setCommand(char* pStr) {

	checkAlloc();

	if(pStr != NULL)
	{
		int len = strlen(pStr);
		if(len > 0)
		{
			char *p = NULL;
			p = strtok(pStr," ");
			int count = 0;
			while(p && count < MAX_STR)
			{
				int nCLen = strlen(p);
				if(nCLen > 0)
				{
					mPstr[count] = new char[nCLen + 1];
					strcpy(mPstr[count],p);

					p = strtok(NULL," ");
					count++;
				}
			}
			nCommandLen = count;
			if(count == 0)
			{
				mPstr[count] = new char[len + 1];
				strcpy(mPstr[count],pStr);
				nCommandLen = count + 1;
			}

		}
	}
}

char* OnCommandJuzEvent::getCommand(int nLine) {

	char *pStr = NULL;
	if(nLine >= 0 && nLine < MAX_STR)
	{
		if(mPstr[nLine] != NULL)
		{
			pStr = mPstr[nLine];
		}
	}
	return pStr;

}

int OnCommandJuzEvent::getLen() {
	return nCommandLen;
}

void OnCommandJuzEvent::checkAlloc() {

	if(mPstr != NULL)
	{
		for(int i = 0 ; i < MAX_STR; i++)
		{
			if(mPstr[i] != NULL)
			{
				delete[] mPstr[i];
				mPstr[i] = NULL;
			}
		}
	}
	nCommandLen = 0;
}
